package com.zhang.redis.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2023-11-30 13:58
 **/
@Slf4j
@RequiredArgsConstructor
@Component
public class RedisHashOpsManager<T> {

    @Resource
    private  RedisTemplate<String, T> redisTemplate;

    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    public HashOperations<String,String,T> getHashOps(){
        return redisTemplate.opsForHash();
    }

    /**
     * 确定hash值是否存在
     * */
    public Boolean hasKey(String key, String hashKey){
        return getHashOps().hasKey(key,hashKey);
    }

    public Set<String> getHashKey(String key){
        return  getHashOps().keys(key);
    }
    
    public T get(String key,String hashKey){
        return  getHashOps().get(key,hashKey);
    }

    public List<T> multiGet(String key,List<String> keys){
        return  getHashOps().multiGet(key, keys);
    }

    public Long increment(String key,String hashKey){
        return  getHashOps().increment(key,hashKey,1);
    }

    public Long increment(String key,String hashKey,long delta){
        return  getHashOps().increment(key,hashKey,delta);
    }

    public Set<String> keys(String key){
        return  getHashOps().keys(key);
    }

    public Long size(String key){
        return  getHashOps().size(key);
    }

    public void put(String key,String hashKey,T value){
        getHashOps().put(key,hashKey,value);
    }

    public void putAll(String key, Map<String,T> map){
        getHashOps().putAll(key,map);
    }
    
    public Boolean putIfAbsent(String key,String hashKey, T value){
        return  getHashOps().putIfAbsent(key,hashKey,value);
    }

    public List<T> values(String key) {
        return  getHashOps().values(key);
    }

    public Long delete(String key, List<String> hashKeyList){
        return getHashOps().delete(key,hashKeyList.toArray());
    }

    public Map<String, T> entries(String key) {
        return getHashOps().entries(key);
    }

}
